package controller;


import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import java.util.Timer;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.charliemouse.cambozola.shared.CamStream;
import com.charliemouse.cambozola.shared.ImageChangeEvent;
import com.charliemouse.cambozola.shared.ImageChangeListener;

import net.java.games.input.Controller;

import model.ModelJpegFrameUpdatedEvent;
import model.ModelListener;
import model.ModelPSDRefreshedEvent;
import controls.GamePad;
import controls.GamePadCheckTask;

import GUINew.CompasModule;
import GUINew.ControlsSettingsDialog;
import GUINew.HorizontModule;
import GUINew.MainWindowFrame;
import GUINew.SensorenModule;
import GUINew.StatusModule;
import GUINew.VideoStreamFrame;


public class MainWindowFrameController implements ImageChangeListener
{
   private MainWindowFrame window;
   private VideoStreamFrame videoFrame;
   private Timer           gamePadTask;
   private Controller         gamePad;

   private CamStream m_imgStream = null;
 

   public void start()
   {
      URL url;
      try
      {
         URL m_mainURL = new URL("http://192.168.0.100:8080/?action=streaming");
         
         m_imgStream = new CamStream(m_mainURL,"1", m_mainURL, 1, 1000, false);
         m_imgStream.addImageChangeListener(this);
         m_imgStream.start();
         
      } catch (MalformedURLException e1)
      {
         // TODO Auto-generated catch block
         System.err.println("Problem mit stream url");
         e1.printStackTrace();
      }
      
      EventQueue.invokeLater(new Runnable()
      {

         public void run()
         {
            try
            {

               initializeGUI();
               registerListener();

               // GamePad initialisieren
               
               if (!GamePad.isGamePadConnected())
               {
                  JOptionPane.showMessageDialog(null, "GamePad ist nicht verbunden!",
                        "", JOptionPane.ERROR_MESSAGE);
               }
               else
               {
                  gamePad = GamePad.getGamePadInstance();
                  startGamePadThread();
               }

            } catch (Exception e)
            {
               e.printStackTrace();
            }
         }
      });
   }

   public void initializeGUI()
   {

      // Hauptprogramm Frame
      window = new MainWindowFrame();

      // Videoframe
      videoFrame = new VideoStreamFrame(m_imgStream);
      videoFrame.setVisible(true);
      window.getVideoStreamPanel().add(videoFrame);
      window.getVideoStreamPanel().revalidate();

      /**
       * Navigation Panel
       */

      // Compasmodule
      CompasModule compasPanel = new CompasModule();
      compasPanel.setVisible(true);
      window.getNavigationPanel().add(compasPanel);

      // Horizontmodule
      HorizontModule neigungPanel = new HorizontModule();
      neigungPanel.setVisible(true);
      window.getNavigationPanel().add(neigungPanel);

      /**
       * Statuspanel
       */

      // Sensorenmodule
      SensorenModule sensorenPanel = new SensorenModule();
      sensorenPanel.setVisible(true);
      window.getStatusPanel().add(sensorenPanel);

      // Statusmodul
      StatusModule statusPanel = new StatusModule();
      statusPanel.setVisible(true);
      window.getStatusPanel().add(statusPanel);

   }

   public void registerListener()
   {
      window.getControlMenuItem().addActionListener(new ActionListener()
      {

         @Override
         public void actionPerformed(ActionEvent e)
         {

            SettingsDialogController settingController = new SettingsDialogController(
                  window.getFrmRovControl());
            settingController.show();

         }
      });
   }

   public void unregisterListener()
   {

   }

   public void refreshGUI()
   {

   }

   private void startGamePadThread()
   {
      // gamePadTask = new Timer();
      // gamePadTask.schedule(new GamePadCheckTask(), 0, PCD_PERIOD);
      // System.out.println("GamePad Thread gestartet");
   }

   @Override
   public void imageChanged(ImageChangeEvent ie)
   {
      // TODO Auto-generated method stub
      if (videoFrame != null)
      {
         videoFrame.update(videoFrame.getPanel().getGraphics());
         videoFrame.getToolkit().sync();
      }
      
      System.out.println("imageChanged");
   }

}
